// Load jQuery
google.load("jquery", "1.3.2");

// Gadget Init
gadgets.util.registerOnLoadHandler(function() {

	// Start things off
	var gadget = new Gadget();
	gadget.init();

});
var GADGET_URL = 'http://actionforwomen.googlecode.com/svn/trunk/'

//var GADGET_URL = 'http://sandbox.essence-media.com/afw-zanete/';
var Gadget = function()
{
	this.questionsEn = [
			 	{id: 1,
				 text: 'Do you know any women who have suffered physical or verbal abuse?',
				 helpTitle: 'Action for Women',
				 img: 'styles/en/images/q1.gif',
				 helpText: '"In the 19th century the paramount moral challenge was slavery. In the 20th century, it was totalitarianism. In this century, it is the brutality inflicted on so many women and girls around the globe: sex trafficking, acid attacks, bride burnings and mass rape" (The New York Times - Aug 23rd 2009).<br /><br />Every day, around the world, millions of women are the victims of violence and discrimination. Please answer our survey to tell us how this social issue is perceived in your country. Help us break the silence and take actions against abuse.',
				 questions: [{val: 'a',
						text: 'Yes, Physical'},
					      {val: 'b',
						text: 'Yes, Verbal'},
					      {val: 'c',
						text: 'Yes, Both'},
					      {val: 'd',
						text: 'No'}]
				},
			 	{id: 2,
				 text: 'What form of violence against women is least publicized in your Country?',
				 helpTitle: 'Action for Women',
				 img: 'styles/en/images/q2.gif',
				 helpText: '"In the 19th century the paramount moral challenge was slavery. In the 20th century, it was totalitarianism. In this century, it is the brutality inflicted on so many women and girls around the globe: sex trafficking, acid attacks, bride burnings and mass rape" (The New York Times - Aug 23rd 2009).<br /><br />Every day, around the world, millions of women are the victims of violence and discrimination. Please answer our survey to tell us how this social issue is perceived in your country. Help us break the silence and take actions against abuse.',
				 questions: [{val: 'a',
						text: 'Domestic violence'},
					      {val: 'b',
						text: 'Mobbing (discrimination at work)'},
					      {val: 'c',
						text: 'Stalking'},
					      {val: 'd',
						text: 'Social and cultural discrimination'}]
				},
				{id: 3,
				 text: 'Why do you think many women do not report abuse?',
				 helpTitle: 'Action for Women',
				 img: 'styles/en/images/q3.gif',
				 helpText: '"In the 19th century the paramount moral challenge was slavery. In the 20th century, it was totalitarianism. In this century, it is the brutality inflicted on so many women and girls around the globe: sex trafficking, acid attacks, bride burnings and mass rape" (The New York Times - Aug 23rd 2009).<br /><br />Every day, around the world, millions of women are the victims of violence and discrimination. Please answer our survey to tell us how this social issue is perceived in your country. Help us break the silence and take actions against abuse.',
				 questions: [{val: 'a',
						text: 'They are ashamed'},
					      {val: 'b',
						text: 'The law does not protect them'},
					      {val: 'c',
						text: 'Fear of legal recourse'},
					      {val: 'd',
						text: 'Fear of their attacker'}]
				},
				{id: 4,
				 text: 'What should the State do to encourage women to report abuse?',
				 helpTitle: 'Action for Women',
				 img: 'styles/en/images/q4.gif',
				 helpText: '"In the 19th century the paramount moral challenge was slavery. In the 20th century, it was totalitarianism. In this century, it is the brutality inflicted on so many women and girls around the globe: sex trafficking, acid attacks, bride burnings and mass rape" (The New York Times - Aug 23rd 2009).<br /><br />Every day, around the world, millions of women are the victims of violence and discrimination. Please answer our survey to tell us how this social issue is perceived in your country. Help us break the silence and take actions against abuse.',
				 questions: [{val: 'a',
						text: 'Creating "public" centers'},
					      {val: 'b',
						text: 'Spreading the word with more conferences/initiatives'},
					      {val: 'c',
						text: 'Creating a special police task force'},
					      {val: 'd',
						text: 'More severe penalties for attackers'}]
				}
			];

	this.questionsIt = [
			 	{id: 1,
				 text: 'Conosci donne che hanno subito maltrattamenti fisici o verbali?',
				 helpTitle: 'Action for Women',
				 img: 'styles/it/images/q1.gif',
				 helpText: '&quot;Nel XIX secolo la sfida dell\'umanit&#224; era eliminare la schiavit&#249;. Nel XX secolo, il totalitarismo. Nel nostro secolo, la battaglia fondamentale &#232; quella contro la brutalit&#224; che troppe donne sono costrette a subire in tutto il mondo: traffico sessuale, aggressioni con l\'acido, spose bruciate vive e stupri di massa&quot;. (New York Times - 23 agosto 2009).<br /><br />Ogni giorno, in tutto il mondo, milioni di donne sono vittime di violenza e discriminazione. Rispondi al nostro questionario per raccontarci come viene percepito questo problema nel tuo Paese. Aiutaci a rompere il silenzio e ad agire contro gli abusi sulle donne.',

				 questions: [{val: 'a',
						text: 'Si, fisici'},
					      {val: 'b',
						text: 'Si, verbali'},
					      {val: 'c',
						text: 'Sia fisici sia verbali'},
					      {val: 'd',
						text: 'No, non ne conosco'}]
				},
			 	{id: 2,
				 text: 'Quale � la forma di violenza verso le donne su cui c\'� meno informazione nel tuo Paese?',
				 helpTitle: 'Action for Women',
				 img: 'styles/it/images/q2.gif',
				 helpText: '&quot;Nel XIX secolo la sfida dell\'umanit&#224; era eliminare la schiavit&#249;. Nel XX secolo, il totalitarismo. Nel nostro secolo, la battaglia fondamentale &#232; quella contro la brutalit&#224; che troppe donne sono costrette a subire in tutto il mondo: traffico sessuale, aggressioni con l\'acido, spose bruciate vive e stupri di massa&quot;. (New York Times - 23 agosto 2009).<br /><br />Ogni giorno, in tutto il mondo, milioni di donne sono vittime di violenza e discriminazione. Rispondi al nostro questionario per raccontarci come viene percepito questo problema nel tuo Paese. Aiutaci a rompere il silenzio e ad agire contro gli abusi sulle donne.',
				 questions: [{val: 'a',
						text: 'Violenza Domestica'},
					      {val: 'b',
						text: 'Mobbing (discriminazione sul lavoro)'},
					      {val: 'c',
						text: 'Stalking (attenzioni moleste)'},
					      {val: 'd',
						text: 'Discriminazione sociale e culturale'}]
				},
			 	{id: 3,
				 text: 'Per quali motivi, secondo te, molte donne non denunciano le violenze?',
				 helpTitle: 'Action for Women',
				 img: 'styles/it/images/q3.gif',
				 helpText: '&quot;Nel XIX secolo la sfida dell\'umanit&#224; era eliminare la schiavit&#249;. Nel XX secolo, il totalitarismo. Nel nostro secolo, la battaglia fondamentale &#232; quella contro la brutalit&#224; che troppe donne sono costrette a subire in tutto il mondo: traffico sessuale, aggressioni con l\'acido, spose bruciate vive e stupri di massa&quot;. (New York Times - 23 agosto 2009).<br /><br />Ogni giorno, in tutto il mondo, milioni di donne sono vittime di violenza e discriminazione. Rispondi al nostro questionario per raccontarci come viene percepito questo problema nel tuo Paese. Aiutaci a rompere il silenzio e ad agire contro gli abusi sulle donne.',
				 questions: [{val: 'a',
						text: 'Vergogna'},
					      {val: 'b',
						text: 'Mancanza di tutela da parte della legge'},
					      {val: 'c',
						text: 'Timore di una eventuale causa legale'},
					      {val: 'd',
						text: 'Timore della persona accusata'}]
				},
			 	{id: 4,
				 text: 'Cosa dovrebbe fare lo Stato per indurre le donne a denunciare i maltrattamenti subiti?',
				 helpTitle: 'Action for Women',
				 img: 'styles/it/images/q4.gif',
				 helpText: '&quot;Nel XIX secolo la sfida dell\'umanit&#224; era eliminare la schiavit&#249;. Nel XX secolo, il totalitarismo. Nel nostro secolo, la battaglia fondamentale &#232; quella contro la brutalit&#224; che troppe donne sono costrette a subire in tutto il mondo: traffico sessuale, aggressioni con l\'acido, spose bruciate vive e stupri di massa&quot;. (New York Times - 23 agosto 2009).<br /><br />Ogni giorno, in tutto il mondo, milioni di donne sono vittime di violenza e discriminazione. Rispondi al nostro questionario per raccontarci come viene percepito questo problema nel tuo Paese. Aiutaci a rompere il silenzio e ad agire contro gli abusi sulle donne.',
				 questions: [{val: 'a',
						text: 'Istituire centri di ascolto "pubblici"'},
					      {val: 'b',
						text: 'Fare piu comunicazione con convegni/iniziative dedicate'},
					      {val: 'c',
						text: 'Istituire un corpo speciale di polizia dedicato'},
					      {val: 'd',
						text: 'Pene piu severe per gli accusati'}]
				}
			];

	this.currentQuestion = 1;
    this.gadgetUrl = GADGET_URL;
	this.serviceUrl = 'http://venice-film-festival-poll.appspot.com/';
	
	this.questions = null;

	this.init = function()
	{
		// Add Questions
		this.initQuestions();

		// Setup event listeners
		this.initEvents();
	}

	this.initQuestions = function()
	{
		switch(lang)
		{
			case 'en':
				this.questions = this.questionsEn;
				break;
			case 'it':
				this.questions = this.questionsIt;
				break;
		}

		var _self = this;		
		var questionsContainer = $('#questions');

		var numQuestions = this.questions.length;

		$.each(this.questions, function(i, val){
			var i = i + 1;
			var answers = '';
			$.each(val.questions, function(e, val){
				answers += '<li><a id="' + i + '-' + val.val + '" href="#' + i + '-' + val.val + '"><span class="result"><span class="percentage"></span><span class="bar"></span></span>' + val.text + '</a></li>';
			});
			var html = '<li id="q' + i + '" class="question"><div class="questions"><img src="' + _self.gadgetUrl + val.img + '" alt="' + val.text + '" /><ul>' + answers + '</ul><div class="loader"></div></div><div class="results"></div><div class="description"><h2>' + val.helpTitle + '</h2><p>' + val.helpText + '</p><div></li>';
			questionsContainer.append(html);
		});
		$('.questions a').click(function(response){
			_self.voteQuestion(response);
			return false;
		});

		this.changeQuestion(1);
	}

	this.initEvents = function()
	{
		var _self = this;

		$('a').click(function(){
			this.blur();
		});	

		// Hijacks the Competition gadget tabs
		// I know this is disgusting, it was originally beautiful... But cross browser hacks made it this way!
		if($("#competition_gadget").get(0).contentDocument) // Non-IE Browsers
		{
			$("#competition_gadget").load(function(){
				if (lang == 'en'){
					$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/home_bg.jpg)';
				}
				if ($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).addEventListener) {
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-nav"))
					{
						if (lang == 'en'){
							var html = '<embed height="380" width="470" src="http://youtube.com/v/ROAqMTVcjM0&amp;enablejsapi=1&amp;rel=0&amp;playerapiid=ytcp-home-video-obj&amp;autoplay=1" type="application/x-shockwave-flash" base="http://youtube.com/v/" allowscriptaccess="always" wmode="opaque" id="ytcp-home-video-obj" autoplay="true" register-view="true"/>';
							$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-video").innerHTML = html;
						}
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
							$('#rules').show();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/home_bg.jpg)';
							}
						}, false);
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-submit-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-submit-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
							}
							$('#rules').show();
						}, false);
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-links"))
					{
						if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-submit-nav")){
							$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-links").getElementsByTagName('a').item(0).addEventListener('click', function(){
								$('#survey_gadget').hide();
								if (lang == 'en'){
									$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
								}
								$('#rules').hide();
							}, false);
						}
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-vote-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-vote-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						}, false);						
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-gallery-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-gallery-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						}, false);
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-results-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-results-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						}, false);
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-custom-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').show();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						}, false);
						if(lang == 'it')
						{
							$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).innerHTML = 'Sondaggio';
						}
					}
				} else if ($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).attachEvent) {
					$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-nav")
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-nav"))
					{
						if (lang == 'en'){
							var html = '<embed height="380" width="470" src="http://youtube.com/v/ROAqMTVcjM0&amp;enablejsapi=1&amp;rel=0&amp;playerapiid=ytcp-home-video-obj&amp;autoplay=1" type="application/x-shockwave-flash" base="http://youtube.com/v/" allowscriptaccess="always" wmode="opaque" id="ytcp-home-video-obj" autoplay="true" register-view="true"/>';
							$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-video").innerHTML = html;
						}
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/home_bg.jpg)';
							}
							$('#rules').show();
						});
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-submit-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-submit-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
							}
							$('#rules').show();
						});
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-links"))
					{
						if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-submit-nav")){
							$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home-links").getElementsByTagName('a').item(0).attachEvent('click', function(){
								$('#survey_gadget').hide();
								if (lang == 'en'){
									$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
								}
								$('#rules').hide();
							}, false);
						}
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-vote-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-vote-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						});
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-gallery-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-gallery-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						});
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-results-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-results-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						});
					}
					if($("#competition_gadget").get(0).contentDocument.getElementById("ytcp-custom-nav"))
					{
						$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').show();
							$("#competition_gadget").get(0).contentDocument.body.style.backgroundImage = null;
							$('#rules').hide();
						});
						if(lang == 'it')
						{
							$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).innerHTML = 'Sondaggio';
						}
					}
				}

				$("#competition_gadget").get(0).contentDocument.getElementById("ytcp-home").style.border = 'none';
				$('#wrapper').show();
				$('#modal').fadeOut(1000);
			});
		}
		else // IE Browsers
		{		
			$("#competition_gadget").load(function(){
				if (lang == 'en'){
					$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/home_bg.jpg)';
				}
				if ($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).addEventListener) {
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-nav"))
					{
						if (lang == 'en'){
							var html = '<embed height="380" width="470" src="http://youtube.com/v/ROAqMTVcjM0;enablejsapi=1&amp;rel=0&amp;playerapiid=ytcp-home-video-obj&amp;autoplay=1" type="application/x-shockwave-flash" base="http://youtube.com/v/" allowscriptaccess="always" wmode="opaque" id="ytcp-home-video-obj" autoplay="true" register-view="true"/>';
							$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-video").innerHTML = html;
						}
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
							$('#rules').show();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/home_bg.jpg)';
							}
						}, false);
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-submit-nav"))
					{
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-submit-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
							$('#rules').show();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
							}
						}, false);
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-links"))
					{
						if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-submit-nav")){
							$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-links").getElementsByTagName('a').item(0).addEventListener('click', function(){
								$('#survey_gadget').hide();
								$('#rules').hide();
								if (lang == 'en'){
									$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
								}
							}, false);
						}
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-vote-nav"))
					{
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-vote-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').hide();
								$('#rules').hide();
							$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = null;
						}, false);
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-custom-nav"))
					{
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).addEventListener('click', function(){
							$('#survey_gadget').show();
							$('#rules').hide();
							$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = null;
						}, false);
						if(lang == 'it')
						{
							$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).innerHTML = 'Sondaggio';
						}
					}
				} else if ($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).attachEvent) {
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-nav"))
					{
						if (lang == 'en'){
							var html = '<embed height="380" width="470" src="http://youtube.com/v/ROAqMTVcjM0&amp;enablejsapi=1&amp;rel=0&amp;playerapiid=ytcp-home-video-obj&amp;autoplay=1" type="application/x-shockwave-flash" base="http://youtube.com/v/" allowscriptaccess="always" wmode="opaque" id="ytcp-home-video-obj" autoplay="true" register-view="true"/>';
							$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-video").innerHTML = html;
						}
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							$('#rules').show();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/home_bg.jpg)';
							}
						});
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-submit-nav"))
					{
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-submit-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							$('#rules').show();
							if (lang == 'en'){
								$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
							}
						});
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-links"))
					{
						if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-submit-nav")){
							$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home-links").getElementsByTagName('a').item(0).attachEvent('click', function(){
								$('#survey_gadget').hide();
								$('#rules').hide();
								if (lang == 'en'){
									$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = 'url(/ig/proxy?url='+GADGET_URL+'styles/en/images/submit_bg.jpg)';
								}
							}, false);
						}
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-vote-nav"))
					{
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-vote-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').hide();
							$('#rules').hide();
							$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = '';
						});
					}
					if($("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-custom-nav"))
					{
						$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).attachEvent('onclick', function(){
							$('#survey_gadget').show();
							$('#rules').hide();
							$("#competition_gadget").get(0).contentWindow.document.body.style.backgroundImage = '';
						});
						if(lang == 'it')
						{
							$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-custom-nav").getElementsByTagName('a').item(0).innerHTML = 'Sondaggio';
						}
					}
				}

				$("#competition_gadget").get(0).contentWindow.document.getElementById("ytcp-home").style.border = 'none';
				$('#wrapper').show();
				$('#modal').fadeOut(1000);
			});
		}
	}

	this.voteQuestion = function(response)
	{
		var string = response.currentTarget.hash.split('#');
		var parts = string[1].split('-');
		var question = parts[0];
		var answer = parts[1];
		var request = this.serviceUrl + 'questions/vote/q/' + question + '/a/' + answer;
		var _self = this;

        //ADD E-COMMERCE TRACKING
        //if(ga) {
            var PageView = '/'+question + '/' + answer;
            _IG_Analytics("UA-9145526-4", PageView);
            //ga.reportPageview(PageView);
            //ga.reportEvent(question, question+':'+answer, lang);
            if(window.console) {
                console.log(PageView);
            }
        //} else {
        //    if(window.console) {
        //        console.log('No ga');
        //    }
        //}
		
		$('#message').fadeOut();
		$('#q' + question + ' .questions ul').fadeOut(function(){
			$('#q' + question + ' .loader').fadeIn(function(){
				gadgets.io.makeRequest(request, function(response){
					data = eval('(' + response.data + ')');
					if (typeof data == 'object')
					{
						$('#' + question + '-' + answer).addClass('checked');
						$('#' + question + '-' + answer).parent().parent().addClass('disabled');
						$('#q' + question + ' .questions li a').css('cursor', 'default').unbind('click').click(function(){
							$(this).blur();
							return false;
						});
						$('#q' + question + ' .loader').fadeOut(function(){
							$('#q' + question + ' .description').fadeOut(function(){
								$('#q' + question + ' .questions ul').fadeIn();
								_self.displayResults(question, data);								
							});
						});
					}
					else
					{
						$('#q' + question + ' .loader').fadeOut(function(){
							$('#q' + question + ' .questions ul').fadeIn();
						});
						$('#message').html('Oops, there was a problem submitting your vote').fadeIn();
					}
				});
			});
		});

		return false;
	}

	this.displayResults = function(question, results)
	{
		$.each(results.Questions[0].Answers, function(i, val){
			$('#' + question + '-' + val.Text + ' .result .percentage').html(val.Percent + '%');
			$('#' + question + '-' + val.Text + ' .result .bar').animate({width: val.Percent * 3 + 'px'}, 700);
		});

		$('#q' + question + ' .result').fadeIn();

		return false;
	}

	this.changeQuestion = function(question)
	{
		$('.question').hide();
		$('#q' + question).show();
		this.currentQuestion = question;
		this.refreshPaging();
	}

	this.getNextQuestion = function()
	{
		var question = this.currentQuestion + 1

		if (question > this.questions.length)
		{
			question = null;
		}

		return question;
	}

	this.getPrevQuestion = function()
	{
		var question = this.currentQuestion - 1

		if (question < 1)
		{
			question = null;
		}

		return question;
	}

	this.refreshPaging = function()
	{
		var _self = this;

		if (this.getPrevQuestion() == null)
		{
			$('#prev_btn').hide();
		}
		else
		{
			$('#prev_btn').show();
		}

		if (this.getNextQuestion() == null)
		{
			$('#next_btn').hide();
		}
		else
		{
			$('#next_btn').show();
		}

		$('#prev_btn').unbind('click').click(function(){
			_self.changeQuestion(_self.getPrevQuestion());
			return false;
		});

		$('#next_btn').unbind('click').click(function(){
			_self.changeQuestion(_self.getNextQuestion());
			return false;
		});
	}
}